package com.jrwd.recycletest.util;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jrwd.okhttputils.OkHttpUtils;
import com.jrwd.okhttputils.callback.StringCallback;
import com.jrwd.recycletest.Api;
import com.jrwd.recycletest.MyApp;
import com.jrwd.recycletest.mange.Constant;
import com.jrwd.recycletest.mange.UserCache;

import okhttp3.Request;
import okhttp3.Response;



/**
 *
 * @author Koma
 * @date 2017/11/1
 * Describe: 统计工具类
 */

public class StatisticsUtils {

    /**
     * * 登录注册统计
     * @param context
     * @param oper_type   1 注册  2登录
     */
    public static void totalLogin(Context context, int oper_type){

        String udid = DeviceUtil.getUniqueId(context);

        PostParamsBuilder  build = new PostParamsBuilder()
                .setParams("udid", udid)
                .setParams("oper_type", oper_type)
                .setParams("product_id", "")
                .setParams("product_type", "")
                .setParams("stay_time", "")
                .setParams("link", "")
                .setParams("is_close", "");

        postData(context, build);
    }

    /**
     * 上报设备激活、日活跃
     * @param context
     * @param type  1--设备激活   2--日活跃
     */
    public static void totalDevice(Context context, int type){

        String udid = "";

        if(type == 1){
            udid = DeviceUtil.getUniqueId(context)+"_xinzeng";
        }else{
            udid = DeviceUtil.getUniqueId(context)+"_rihuo";
        }

        PostParamsBuilder  build = new PostParamsBuilder()
                .setParams("udid", udid)
                .setParams("oper_type", "3")
                .setParams("product_id", "")
                .setParams("product_type", "")
                .setParams("stay_time", "")
                .setParams("link", "")
                .setParams("is_close", "");

        postData(context, build);

    }

    /**
     * 上报产品申请、产品访问、弹框点击
     * @param context
     * @param oper_type      4 产品申请   5 产品查看（进去就调）   6点击弹框
     * @param product_id     产品id
     * @param product_type   产品类型 1表示贷款产品  2表示信用卡产品
     * @param stay_time      产品访问停留时长
     * @param link           是否跳转链接       0 无跳转  1跳转
     * @param is_jump        是否跳转应用市场   1 跳转  2 无跳转
     * @param glide_height    滑动高度
     * @param is_close       是否点击了弹框  1 手动关闭了弹框  2 自动推荐跳走   3 今日不在提示
     */
    public static void TotalProduct(Context context, int oper_type, String product_id, String product_type,
                                    String stay_time, String link, String is_jump, String glide_height,
                                    String is_close){
        int lable_category = (int) SpUtils.get(context,"lable_category",0);
        PostParamsBuilder  build = new PostParamsBuilder()
                .setParams("udid", DeviceUtil.getUniqueId(context))
                .setParams("oper_type", oper_type)
                .setParams("product_id", product_id)
                .setParams("product_type", product_type)
                .setParams("stay_time", stay_time)
                .setParams("link", link)
                .setParams("is_close", is_close)
                .setParams("is_jump", is_jump)
                .setParams("lable_category",lable_category)
                .setParams("glide_height",glide_height);
        postData(context, build);
    }

    /**
     * 上报统计数据
     * @param context
     * @param postParamsBuilder
     */
    private static void postData(Context context, PostParamsBuilder postParamsBuilder){

        String user_id = UserCache.getId(context);
        String app_no = Constant.APP_NUM;
        String channel_no = "";
        String app_type = "";

        QTLog.e("platform-->"+UserCache.getPlatform(context));
        QTLog.e("channel-->"+UserCache.getChannel(context));

        if(!TextUtils.isEmpty(UserCache.getPlatform(context))){
            if(UserCache.getPlatform(context).equals("1")){
                channel_no = MyApp.channel;
                app_type = UserCache.getPlatform(context);
            }else if(UserCache.getPlatform(context).equals("2")){
                if(!TextUtils.isEmpty(UserCache.getChannel(context))){
                    channel_no = UserCache.getChannel(context);
                    app_type = UserCache.getPlatform(context);
                }else{
                    channel_no = MyApp.channel;
                    app_type = "1";
                }
            }
        }else{
            channel_no = MyApp.channel;
            app_type = "1";
        }

        String data = postParamsBuilder
                .setParams("user_id", user_id)
                .setParams("channel_no", channel_no)
                .setParams("app_type", app_type)
                .setParams("app_no", app_no).build();

        if (!NetUtils.isConnected(context)) {
            ToastUtils.showShort(context, "没有网络");
            return;
        }
        OkHttpUtils.post(Api.TJ_INDEX)
                .params("data", data)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean b, String s, Request request, @Nullable Response response) {

                    }
                });
    }


    /**
     * 埋点统计
     * @param context
     */
    public static void tjPoint(final Context context){

        String user_id = UserCache.getId(context);
        String udid = DeviceUtil.getUniqueId(context);

        JSONObject jsonObject = new JSONObject();
        if (SpUtils.contains(context,"type_1")){
            JSONObject json = new JSONObject();
            json.put("type",1);
            json.put("is_register",0);
            jsonObject.put("1",json);
        }
        if (SpUtils.contains(context,"type_2")){
            JSONObject json = new JSONObject();
            json.put("type",2);
            json.put("is_register",0);
            jsonObject.put("2",json);
        }
        if (SpUtils.contains(context,"type_3")){
            JSONObject json = new JSONObject();
            json.put("type",3);
            json.put("is_register",0);
            jsonObject.put("3",json);
        }
        if (SpUtils.contains(context,"type_4")){
            JSONObject json = new JSONObject();
            json.put("type",4);
            json.put("is_register",0);
            jsonObject.put("4",json);
        }
        if (SpUtils.contains(context,"type_5")){
            JSONObject json = new JSONObject();
            json.put("type",5);
            json.put("is_register",SpUtils.get(context,"is_register",1));
            jsonObject.put("5",json);
        }

        if (!jsonObject.isEmpty()){
            String data = Constant.paramsBuilder(context)
                    .setParams("user_id", user_id)
                    .setParam(udid,jsonObject.toJSONString())
                    .build();

            OkHttpUtils.post(Api.TJ_POINT)
                    .params(Constant.SERVER_PARAM,data)
                    .execute(new StringCallback() {
                        @Override
                        public void onResponse(boolean b, String s, Request request, @Nullable Response response) {
                            SpUtils.remove(context,"type_1");
                            SpUtils.remove(context,"type_2");
                            SpUtils.remove(context,"type_3");
                            SpUtils.remove(context,"type_4");
                            SpUtils.remove(context,"type_5");
                            SpUtils.remove(context,"is_register");
                        }
                    });
        }

    }

    /**
     * 日活跃统计
     * @param context
     */
    public static void dayActive(final Context context){

        String user_id = UserCache.getId(context);
        String channel_no = "";
        String app_type = "";

        QTLog.e("platform-->"+UserCache.getPlatform(context));
        QTLog.e("channel-->"+UserCache.getChannel(context));

        if(!TextUtils.isEmpty(UserCache.getPlatform(context))){
            if(UserCache.getPlatform(context).equals("1")){
                channel_no = MyApp.channel;
                app_type = UserCache.getPlatform(context);
            }else if(UserCache.getPlatform(context).equals("2")){
                if(!TextUtils.isEmpty(UserCache.getChannel(context))){
                    channel_no = UserCache.getChannel(context);
                    app_type = UserCache.getPlatform(context);
                }else{
                    channel_no = MyApp.channel;
                    app_type = "1";
                }
            }
        }else{
            channel_no = MyApp.channel;
            app_type = "1";
        }

        String json = Constant.paramsBuilder(context)
                .setParams("user_id", user_id)
                .setParams("app_version",VersionUtil.getVersionName(context))
                .setParams("udid",DeviceUtil.getUniqueId(context))
                .setParams("device_company",DeviceUtil.getDeviceBrand())
                .setParams("device_model",DeviceUtil.getSystemModel())
                .setParams("device_type",2)
                .setParams("channel_no",channel_no)
                .setParams("app_type",app_type)
                .build();

        OkHttpUtils.post(Api.DAY_ACTIVE)
                .params(Constant.SERVER_PARAM,json)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean b, String s, Request request, @Nullable Response response) {
                        SpUtils.put(context,"day_active", StringUtils.getDate());
                    }
                });
    }

    public static void tjLabel(final Context context){

        JSONArray jsonArray = new JSONArray();
        if (SpUtils.contains(context,"tj_label")){
            String label = (String) SpUtils.get(context,"tj_label","");
            if (!TextUtils.isEmpty(label)){
                for (int i = 0;i < label.length();i++){
                    jsonArray.add(Integer.parseInt(label.substring(i,i+1)));
                }
                QTLog.d("yxb----jsonArray-->"+ jsonArray.toString());
                String data = Constant.paramsBuilder(context)
                        .setParam("type",jsonArray.toJSONString())
                        .build();
                OkHttpUtils.post(Api.TJ_LABEL)
                        .params(Constant.SERVER_PARAM,data)
                        .execute(new StringCallback() {
                            @Override
                            public void onResponse(boolean b, String s, Request request, @Nullable Response response) {
                                SpUtils.remove(context,"tj_label");
                            }
                        });
            }
        }

    }


    /**
     * 浏览历史记录
     * @param context
     */
    public static void proBrowse(final Context context, String proId){

        String user_id = UserCache.getId(context);
        String channel_no = "";
        String app_type = "";


        if(!TextUtils.isEmpty(UserCache.getPlatform(context))){
            if(UserCache.getPlatform(context).equals("1")){
                channel_no = MyApp.channel;
                app_type = UserCache.getPlatform(context);
            }else if(UserCache.getPlatform(context).equals("2")){
                if(!TextUtils.isEmpty(UserCache.getChannel(context))){
                    channel_no = UserCache.getChannel(context);
                    app_type = UserCache.getPlatform(context);
                }else{
                    channel_no = MyApp.channel;
                    app_type = "1";
                }
            }
        }else{
            channel_no = MyApp.channel;
            app_type = "1";
        }

        String json = Constant.paramsBuilder(context)
                              .setParams("user_id", user_id)
                              .setParams("app_version",VersionUtil.getVersionName(context))
                              .setParams("udid",DeviceUtil.getUniqueId(context))
                              .setParams("device_company",DeviceUtil.getDeviceBrand())
                              .setParams("device_model",DeviceUtil.getSystemModel())
                              .setParams("device_type",2)
                              .setParams("channel_no",channel_no)
                              .setParams("app_type",app_type)
                              .setParams("product_id",proId)
                              .build();
        OkHttpUtils.post(Api.SET_PRODUCT_VISITOR)
                   .params(Constant.SERVER_PARAM,json)
                   .execute(new StringCallback() {
                       @Override
                       public void onResponse(boolean b, String s, Request request, @Nullable Response response) {

                       }
                   });
    }


    /**
     * 4.3.0 埋点统计
     * @param context
     */
    public static void tjNewPoint(final Context context, final String type){

        String data = Constant.paramsBuilder(context)
                              .setParams("lable_category", type)
                              .build();
        OkHttpUtils.post(Api.TJ_INDEX)
                   .params("data",data)
                   .execute(new StringCallback() {
                       @Override
                       public void onResponse(boolean b, String s, Request request, @Nullable Response response) {

                       }
                   });
    }


    /**
     * 统计模块 - 位置统计
     * @param context
     */
    public static void tjPoisition(final Context context, final String type, int offset){

        String data = Constant.paramsBuilder(context)
                              .setParams("position", type)
                              .setParams("offset", offset)
                              .setParams("channel_no",  MyApp.channel)
                              .setParams("app_no", Constant.APP_NUM)
                              .build();
        OkHttpUtils.post(Api.TJ_TJ_POSITION)
                   .params("data",data)
                   .execute(new StringCallback() {
                       @Override
                       public void onResponse(boolean b, String s, Request request, @Nullable Response response) {

                       }
                   });
    }
}
